﻿using Link_eLab.SqlSugar.Extension;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Nancy.Json;
using Newtonsoft.Json.Linq;
using Configuration.ServerApi.Dao;
using Configuration.ServerApi.Entity;
using Configuration.ServerApi.Enum;
using Configuration.ServerApi.Param;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Configuration.ServerApi.Service.Impl
{
    /// <summary>
    /// AssemblyService
    /// </summary>
    public class AssemblyService : IAssemblyService
    {
        private readonly IConfiguration Configuration;
        private readonly IHttpContextAccessor Accessor;
        private readonly IBisFlowAssemblyClassifyDao BisFlowAssemblyClassifyDao;
        private readonly IBisFlowAssemblyConfigDao BisFlowAssemblyConfigDao;
        private readonly IBisFlowAssemblyConfigAttributeDao BisFlowAssemblyConfigAttributeDao;

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="accessor"></param>
        /// <param name="bisFlowAssemblyClassifyDao"></param>
        /// <param name="bisFlowAssemblyConfigDao"></param>
        /// <param name="bisFlowAssemblyConfigAttributeDao"></param>
        public AssemblyService(IConfiguration configuration,
            IHttpContextAccessor accessor,
            IBisFlowAssemblyClassifyDao bisFlowAssemblyClassifyDao,
            IBisFlowAssemblyConfigDao bisFlowAssemblyConfigDao,
            IBisFlowAssemblyConfigAttributeDao bisFlowAssemblyConfigAttributeDao)
        {
            Configuration = configuration;
            Accessor = accessor;
            BisFlowAssemblyClassifyDao = bisFlowAssemblyClassifyDao;
            BisFlowAssemblyConfigDao = bisFlowAssemblyConfigDao;
            BisFlowAssemblyConfigAttributeDao = bisFlowAssemblyConfigAttributeDao;
        }

        #endregion 构造函数

        #region 获取组件分类列表

        /// <summary>
        /// 获取组件分类列表
        /// </summary>
        /// <param name="param">搜索条件</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> FlowAssemblyClassifyList(SearchModel param)
        {
            Console.WriteLine("\n获取组件分类列表：FlowAssemblyClassifyList");

            #region 获取登录用户相关信息

            string orgId = string.Empty;
            string userId = string.Empty;
            string userRoleId = string.Empty;
            try
            {
                orgId = Accessor.HttpContext.User.Claims.First(i => i.Type == "orgId").Value;
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userRoleId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userRoleId").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            // 初始化数据查询表达式
            Expression<Func<bis_flow_assembly_classify, bool>> expression = w => w.Del_Flag == 0;

            Console.WriteLine($"CnName：{param.CnName}");
            if (!string.IsNullOrEmpty(param.CnName))
            {
                expression = expression.And(w => w.CnName.Contains(param.CnName));
            }

            Console.WriteLine($"EnName：{param.EnName}");
            if (!string.IsNullOrEmpty(param.EnName))
            {
                expression = expression.And(w => w.EnName.Contains(param.EnName));
            }

            Console.WriteLine($"Status：{param.Status}");
            if (!string.IsNullOrEmpty(param.Status))
            {
                string[] conditions = param.Status.Split(',');
                Expression<Func<bis_flow_assembly_classify, bool>> expressionSub = w => w.Status.Equals(conditions.GetValue(0).ToString());
                expressionSub = expressionSub.And(w => w.Del_Flag == 0);
                for (int i = 1; i < conditions.Length; i++)
                {
                    var statusTmp = conditions.GetValue(i).ToString();
                    expressionSub = expressionSub.Or(w => w.Status.Equals(statusTmp));
                }
                expression = expression.And(expressionSub);
            }

            Console.WriteLine($"StartTime：{param.StartTime}");
            Console.WriteLine($"EndTime：{param.EndTime}");
            if (!string.IsNullOrEmpty(param.StartTime) && !string.IsNullOrEmpty(param.EndTime))
            {
                DateTime formatStartTimeStr = DateTime.Parse($"{param.StartTime} 00:00:00");
                DateTime formatEndTimeStr = DateTime.Parse($"{param.EndTime} 23:59:59");

                expression = expression.And(w => w.CreateTime >= formatStartTimeStr && w.CreateTime <= formatEndTimeStr);
            }

            List<bis_flow_assembly_classify> result = new List<bis_flow_assembly_classify>();
            if (param.Page != null && param.Limit != null)
            {
                result = await BisFlowAssemblyClassifyDao.QueryByWherePage(param.Page.Value, param.Limit.Value, expression, o => o.Sort);
            }
            else
            {
                result = await BisFlowAssemblyClassifyDao.QueryByWhere(expression, o => o.Sort);
            }

            var count = await BisFlowAssemblyClassifyDao.QueryCount(expression);

            return new JsonResult(new ResultModel
            {
                Count = count,
                Data = result
            });
        }

        #endregion

        #region 获取组件分类详情

        /// <summary>
        /// 获取组件分类详情
        /// </summary>
        /// <param name="param">搜索条件</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> FlowAssemblyClassifyDetail(SearchModel param)
        {
            Console.WriteLine("\n获取组件分类详情：FlowAssemblyClassifyDetail");

            #region 参数校验

            Console.WriteLine($"Id：{param.Id}");
            if (string.IsNullOrEmpty(param.Id))
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            var result = await BisFlowAssemblyClassifyDao.QuerySingle(w => w.Del_Flag == 0 && w.Id.Equals(param.Id));

            return new JsonResult(new ResultModel
            {
                Data = result
            });
        }

        #endregion

        #region 提交组件分类信息

        /// <summary>
        /// 提交组件分类信息
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> SubmitFlowAssemblyClassify(UpdateModel param)
        {
            Console.WriteLine("\n提交组件分类信息：SubmitFlowAssemblyClassify");

            #region 参数、数据校验

            Console.WriteLine($"FlowAssemblyClassifyData：{param.FlowAssemblyClassifyData}");

            if (param.FlowAssemblyClassifyData == null)
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion 

            #region 获取所需数据

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            string orgId = string.Empty;
            string orgName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
                orgId = Accessor.HttpContext.User.Claims.First(i => i.Type == "orgId").Value;
                orgName = Accessor.HttpContext.User.Claims.First(i => i.Type == "orgName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            #endregion

            #region 数据提交

            DateTime now = DateTime.Now;
            Guid guid = Guid.NewGuid();
            string newId = guid.ToString().ToUpper();

            int result = 0;
            if (string.IsNullOrEmpty(param.FlowAssemblyClassifyData.Id))
            {
                // 填充数据
                param.FlowAssemblyClassifyData.Id = newId;
                param.FlowAssemblyClassifyData.OrgGuid = orgId;
                param.FlowAssemblyClassifyData.OrgName = orgName;
                param.FlowAssemblyClassifyData.CreateUser = userId;
                param.FlowAssemblyClassifyData.CreateUserName = userName;
                param.FlowAssemblyClassifyData.CreateTime = now;
                param.FlowAssemblyClassifyData.UpdateUser = userId;
                param.FlowAssemblyClassifyData.UpdateUserName = userName;
                param.FlowAssemblyClassifyData.UpdateTime = now;
                param.FlowAssemblyClassifyData.Del_Flag = 0;

                result = await BisFlowAssemblyClassifyDao.Insert(param.FlowAssemblyClassifyData);

                return new JsonResult(new ResultModel
                {
                    Msg = "新增成功",
                    Data = result
                });
            }
            else
            {
                // 获取详情
                var detail = await BisFlowAssemblyClassifyDao.QuerySingle(w => w.Del_Flag == 0 && w.Id.Equals(param.FlowAssemblyClassifyData.Id));
                if (detail == null)
                {
                    return new JsonResult(new ResultModel
                    {
                        Success = false,
                        Msg = "未找到此数据"
                    });
                }

                // 填充数据
                param.FlowAssemblyClassifyData.OrgGuid = detail.OrgGuid;
                param.FlowAssemblyClassifyData.OrgName = detail.OrgName;
                param.FlowAssemblyClassifyData.CreateUser = detail.CreateUser;
                param.FlowAssemblyClassifyData.CreateUserName = detail.CreateUserName;
                param.FlowAssemblyClassifyData.CreateTime = detail.CreateTime;
                param.FlowAssemblyClassifyData.UpdateUser = userId;
                param.FlowAssemblyClassifyData.UpdateUserName = userName;
                param.FlowAssemblyClassifyData.UpdateTime = now;
                param.FlowAssemblyClassifyData.Del_Flag = 0;

                result = await BisFlowAssemblyClassifyDao.Update(param.FlowAssemblyClassifyData);

                return new JsonResult(new ResultModel
                {
                    Msg = "编辑成功",
                    Data = result
                });
            }

            #endregion 数据提交
        }

        #endregion

        #region 批量删除组件分类信息

        /// <summary>
        /// 批量删除组件分类信息
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> DelFlowAssemblyClassify(UpdateModel param)
        {
            Console.WriteLine("\n批量删除组件分类信息：DelFlowAssemblyClassify");

            #region 参数效验

            Console.WriteLine($"Id：{param.Id}");

            if (string.IsNullOrEmpty(param.Id))
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            DateTime now = DateTime.Now;
            int result = await BisFlowAssemblyClassifyDao.Update(u => new bis_flow_assembly_classify
            {
                Del_Flag = 1,
                UpdateUser = userId,
                UpdateUserName = userName,
                UpdateTime = DateTime.Now
            }, w => param.Id.Contains(w.Id));

            return new JsonResult(new ResultModel
            {
                Msg = "删除成功",
                Data = result
            });
        }

        #endregion

        #region 更新组件分类信息状态

        /// <summary>
        /// 更新组件分类信息状态
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> UpdateFlowAssemblyClassifyStstus(UpdateModel param)
        {
            Console.WriteLine("\n更新组件分类信息状态：UpdateFlowAssemblyClassifyStstus");

            #region 参数效验

            Console.WriteLine($"Id：{param.Id}");
            Console.WriteLine($"Status：{param.Status}");

            if (string.IsNullOrEmpty(param.Id))
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            DateTime now = DateTime.Now;
            int result = await BisFlowAssemblyClassifyDao.Update(u => new bis_flow_assembly_classify
            {
                Status = param.Status,
                UpdateUser = userId,
                UpdateUserName = userName,
                UpdateTime = DateTime.Now
            }, w => param.Id.Contains(w.Id));

            return new JsonResult(new ResultModel
            {
                Msg = "提交成功",
                Data = result
            });
        }

        #endregion

        #region 更新组件分类信息顺序

        /// <summary>
        /// 更新组件分类信息顺序
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> UpdateFlowAssemblyClassifySort(UpdateModel param)
        {
            await Task.Run(() => { Console.WriteLine("\n更新组件分类信息顺序：UpdateFlowAssemblyClassifySort"); });

            #region 参数效验

            Console.WriteLine($"SetValueData：{param.SetValueData}");

            if (param.SetValueData == null)
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            DateTime now = DateTime.Now;

            int result = 0;
            Parallel.ForEach(param.SetValueData, async item =>
            {
                result += await BisFlowAssemblyClassifyDao.Update(u => new bis_flow_assembly_classify
                {
                    Sort = int.Parse(item.Value)
                }, w => w.Id.Equals(item.Id));
            });

            return new JsonResult(new ResultModel
            {
                Msg = "提交成功",
                Data = result
            });
        }

        #endregion

        #region 获取组件配置列表

        /// <summary>
        /// 获取组件配置列表
        /// </summary>
        /// <param name="param">搜索条件</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> FlowAssemblyConfigList(SearchModel param)
        {
            Console.WriteLine("\n获取组件配置列表：FlowAssemblyConfigList");

            #region 获取登录用户相关信息

            string orgId = string.Empty;
            string userId = string.Empty;
            string userRoleId = string.Empty;
            try
            {
                orgId = Accessor.HttpContext.User.Claims.First(i => i.Type == "orgId").Value;
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userRoleId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userRoleId").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            // 初始化数据查询表达式
            Expression<Func<bis_flow_assembly_config, bool>> expression = w => w.Del_Flag == 0;

            Console.WriteLine($"ClassifyId：{param.ClassifyId}");
            if (!string.IsNullOrEmpty(param.ClassifyId))
            {
                expression = expression.And(w => w.ClassifyId.Contains(param.ClassifyId));
            }

            Console.WriteLine($"ClassifyName：{param.ClassifyName}");
            if (!string.IsNullOrEmpty(param.ClassifyName))
            {
                expression = expression.And(w => w.ClassifyName.Contains(param.ClassifyName));
            }

            Console.WriteLine($"CnName：{param.CnName}");
            if (!string.IsNullOrEmpty(param.CnName))
            {
                expression = expression.And(w => w.CnName.Contains(param.CnName));
            }

            Console.WriteLine($"EnName：{param.EnName}");
            if (!string.IsNullOrEmpty(param.EnName))
            {
                expression = expression.And(w => w.EnName.Contains(param.EnName));
            }

            Console.WriteLine($"Status：{param.Status}");
            if (!string.IsNullOrEmpty(param.Status))
            {
                string[] conditions = param.Status.Split(',');
                Expression<Func<bis_flow_assembly_config, bool>> expressionSub = w => w.Status.Equals(conditions.GetValue(0).ToString());
                expressionSub = expressionSub.And(w => w.Del_Flag == 0);
                for (int i = 1; i < conditions.Length; i++)
                {
                    var statusTmp = conditions.GetValue(i).ToString();
                    expressionSub = expressionSub.Or(w => w.Status.Equals(statusTmp));
                }
                expression = expression.And(expressionSub);
            }

            Console.WriteLine($"StartTime：{param.StartTime}");
            Console.WriteLine($"EndTime：{param.EndTime}");
            if (!string.IsNullOrEmpty(param.StartTime) && !string.IsNullOrEmpty(param.EndTime))
            {
                DateTime formatStartTimeStr = DateTime.Parse($"{param.StartTime} 00:00:00");
                DateTime formatEndTimeStr = DateTime.Parse($"{param.EndTime} 23:59:59");

                expression = expression.And(w => w.CreateTime >= formatStartTimeStr && w.CreateTime <= formatEndTimeStr);
            }

            List<bis_flow_assembly_config> result = new List<bis_flow_assembly_config>();
            if (param.Page != null && param.Limit != null)
            {
                result = await BisFlowAssemblyConfigDao.QueryByWherePage(param.Page.Value, param.Limit.Value, expression, o => o.Sort);
            }
            else
            {
                result = await BisFlowAssemblyConfigDao.QueryByWhere(expression, o => o.Sort);
            }

            var count = await BisFlowAssemblyConfigDao.QueryCount(expression);

            return new JsonResult(new ResultModel
            {
                Count = count,
                Data = result
            });
        }

        #endregion

        #region 获取组件配置详情

        /// <summary>
        /// 获取组件配置详情
        /// </summary>
        /// <param name="param">搜索条件</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> FlowAssemblyConfigDetail(SearchModel param)
        {
            Console.WriteLine("\n获取组件配置详情：FlowAssemblyConfigDetail");

            #region 参数校验

            Console.WriteLine($"Id：{param.Id}");
            if (string.IsNullOrEmpty(param.Id))
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            var result = await BisFlowAssemblyConfigDao.QuerySingle(w => w.Del_Flag == 0 && w.Id.Equals(param.Id));

            return new JsonResult(new ResultModel
            {
                Data = result
            });
        }

        #endregion

        #region 提交组件配置信息

        /// <summary>
        /// 提交组件配置信息
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> SubmitFlowAssemblyConfig(UpdateModel param)
        {
            Console.WriteLine("\n提交组件配置信息：SubmitFlowAssemblyConfig");

            #region 参数、数据校验

            Console.WriteLine($"FlowAssemblyConfigData：{param.FlowAssemblyConfigData}");

            if (param.FlowAssemblyConfigData == null)
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion 

            #region 获取所需数据

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            string orgId = string.Empty;
            string orgName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
                orgId = Accessor.HttpContext.User.Claims.First(i => i.Type == "orgId").Value;
                orgName = Accessor.HttpContext.User.Claims.First(i => i.Type == "orgName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            #endregion

            #region 数据提交

            DateTime now = DateTime.Now;
            Guid guid = Guid.NewGuid();
            string newId = guid.ToString().ToUpper();

            int result = 0;
            if (string.IsNullOrEmpty(param.FlowAssemblyConfigData.Id))
            {
                // 填充数据
                param.FlowAssemblyConfigData.Id = newId;
                param.FlowAssemblyConfigData.OrgGuid = orgId;
                param.FlowAssemblyConfigData.OrgName = orgName;
                param.FlowAssemblyConfigData.CreateUser = userId;
                param.FlowAssemblyConfigData.CreateUserName = userName;
                param.FlowAssemblyConfigData.CreateTime = now;
                param.FlowAssemblyConfigData.UpdateUser = userId;
                param.FlowAssemblyConfigData.UpdateUserName = userName;
                param.FlowAssemblyConfigData.UpdateTime = now;
                param.FlowAssemblyConfigData.Del_Flag = 0;

                result = await BisFlowAssemblyConfigDao.Insert(param.FlowAssemblyConfigData);

                return new JsonResult(new ResultModel
                {
                    Msg = "新增成功",
                    Data = result
                });
            }
            else
            {
                // 获取详情
                var detail = await BisFlowAssemblyConfigDao.QuerySingle(w => w.Del_Flag == 0 && w.Id.Equals(param.FlowAssemblyConfigData.Id));
                if (detail == null)
                {
                    return new JsonResult(new ResultModel
                    {
                        Success = false,
                        Msg = "未找到此数据"
                    });
                }

                // 填充数据
                param.FlowAssemblyConfigData.OrgGuid = detail.OrgGuid;
                param.FlowAssemblyConfigData.OrgName = detail.OrgName;
                param.FlowAssemblyConfigData.CreateUser = detail.CreateUser;
                param.FlowAssemblyConfigData.CreateUserName = detail.CreateUserName;
                param.FlowAssemblyConfigData.CreateTime = detail.CreateTime;
                param.FlowAssemblyConfigData.UpdateUser = userId;
                param.FlowAssemblyConfigData.UpdateUserName = userName;
                param.FlowAssemblyConfigData.UpdateTime = now;
                param.FlowAssemblyConfigData.Del_Flag = 0;

                result = await BisFlowAssemblyConfigDao.Update(param.FlowAssemblyConfigData);

                return new JsonResult(new ResultModel
                {
                    Msg = "编辑成功",
                    Data = result
                });
            }

            #endregion 数据提交
        }

        #endregion

        #region 批量删除组件配置信息

        /// <summary>
        /// 批量删除组件配置信息
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> DelFlowAssemblyConfig(UpdateModel param)
        {
            Console.WriteLine("\n批量删除组件配置信息：DelFlowAssemblyConfig");

            #region 参数效验

            Console.WriteLine($"Id：{param.Id}");

            if (string.IsNullOrEmpty(param.Id))
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            DateTime now = DateTime.Now;
            int result = await BisFlowAssemblyConfigDao.Update(u => new bis_flow_assembly_config
            {
                Del_Flag = 1,
                UpdateUser = userId,
                UpdateUserName = userName,
                UpdateTime = DateTime.Now
            }, w => param.Id.Contains(w.Id));

            return new JsonResult(new ResultModel
            {
                Msg = "删除成功",
                Data = result
            });
        }

        #endregion

        #region 更新组件配置信息状态

        /// <summary>
        /// 更新组件配置信息状态
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> UpdateFlowAssemblyConfigStstus(UpdateModel param)
        {
            Console.WriteLine("\n更新组件配置信息状态：UpdateFlowAssemblyConfigStstus");

            #region 参数效验

            Console.WriteLine($"Id：{param.Id}");
            Console.WriteLine($"Status：{param.Status}");

            if (string.IsNullOrEmpty(param.Id))
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            DateTime now = DateTime.Now;
            int result = await BisFlowAssemblyConfigDao.Update(u => new bis_flow_assembly_config
            {
                Status = param.Status,
                UpdateUser = userId,
                UpdateUserName = userName,
                UpdateTime = DateTime.Now
            }, w => param.Id.Contains(w.Id));

            return new JsonResult(new ResultModel
            {
                Msg = "提交成功",
                Data = result
            });
        }

        #endregion

        #region 更新组件配置信息顺序

        /// <summary>
        /// 更新组件配置信息顺序
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> UpdateFlowAssemblyConfigSort(UpdateModel param)
        {
            await Task.Run(() => { Console.WriteLine("\n更新组件配置信息顺序：UpdateFlowAssemblyConfigSort"); });

            #region 参数效验

            Console.WriteLine($"SetValueData：{param.SetValueData}");

            if (param.SetValueData == null)
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            DateTime now = DateTime.Now;

            int result = 0;
            Parallel.ForEach(param.SetValueData, async item =>
            {
                result += await BisFlowAssemblyConfigDao.Update(u => new bis_flow_assembly_config
                {
                    Sort = int.Parse(item.Value)
                }, w => w.Id.Equals(item.Id));
            });

            return new JsonResult(new ResultModel
            {
                Msg = "提交成功",
                Data = result
            });
        }

        #endregion

        #region 获取组件属性配置列表

        /// <summary>
        /// 获取组件属性配置列表
        /// </summary>
        /// <param name="param">搜索条件</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> FlowAssemblyConfigAttributeList(SearchModel param)
        {
            Console.WriteLine("\n获取组件属性配置列表：FlowAssemblyConfigAttributeList");

            #region 获取登录用户相关信息

            string orgId = string.Empty;
            string userId = string.Empty;
            string userRoleId = string.Empty;
            try
            {
                orgId = Accessor.HttpContext.User.Claims.First(i => i.Type == "orgId").Value;
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userRoleId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userRoleId").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            // 初始化数据查询表达式
            Expression<Func<bis_flow_assembly_config_attribute, bool>> expression = w => w.Del_Flag == 0;

            Console.WriteLine($"AssemblyConfigId：{param.AssemblyConfigId}");
            if (!string.IsNullOrEmpty(param.AssemblyConfigId))
            {
                expression = expression.And(w => w.AssemblyConfigId.Contains(param.AssemblyConfigId));
            }

            Console.WriteLine($"AssemblyConfigName：{param.AssemblyConfigName}");
            if (!string.IsNullOrEmpty(param.AssemblyConfigName))
            {
                expression = expression.And(w => w.AssemblyConfigName.Contains(param.AssemblyConfigName));
            }

            Console.WriteLine($"CnName：{param.CnName}");
            if (!string.IsNullOrEmpty(param.CnName))
            {
                expression = expression.And(w => w.CnName.Contains(param.CnName));
            }

            Console.WriteLine($"EnName：{param.EnName}");
            if (!string.IsNullOrEmpty(param.EnName))
            {
                expression = expression.And(w => w.EnName.Contains(param.EnName));
            }

            Console.WriteLine($"Status：{param.Status}");
            if (!string.IsNullOrEmpty(param.Status))
            {
                string[] conditions = param.Status.Split(',');
                Expression<Func<bis_flow_assembly_config_attribute, bool>> expressionSub = w => w.Status.Equals(conditions.GetValue(0).ToString());
                expressionSub = expressionSub.And(w => w.Del_Flag == 0);
                for (int i = 1; i < conditions.Length; i++)
                {
                    var statusTmp = conditions.GetValue(i).ToString();
                    expressionSub = expressionSub.Or(w => w.Status.Equals(statusTmp));
                }
                expression = expression.And(expressionSub);
            }

            Console.WriteLine($"StartTime：{param.StartTime}");
            Console.WriteLine($"EndTime：{param.EndTime}");
            if (!string.IsNullOrEmpty(param.StartTime) && !string.IsNullOrEmpty(param.EndTime))
            {
                DateTime formatStartTimeStr = DateTime.Parse($"{param.StartTime} 00:00:00");
                DateTime formatEndTimeStr = DateTime.Parse($"{param.EndTime} 23:59:59");

                expression = expression.And(w => w.CreateTime >= formatStartTimeStr && w.CreateTime <= formatEndTimeStr);
            }

            List<bis_flow_assembly_config_attribute> result = new List<bis_flow_assembly_config_attribute>();
            if (param.Page != null && param.Limit != null)
            {
                result = await BisFlowAssemblyConfigAttributeDao.QueryByWherePage(param.Page.Value, param.Limit.Value, expression, o => o.Sort);
            }
            else
            {
                result = await BisFlowAssemblyConfigAttributeDao.QueryByWhere(expression, o => o.Sort);
            }

            var count = await BisFlowAssemblyConfigAttributeDao.QueryCount(expression);

            return new JsonResult(new ResultModel
            {
                Count = count,
                Data = result
            });
        }

        #endregion

        #region 获取组件属性配置详情

        /// <summary>
        /// 获取组件属性配置详情
        /// </summary>
        /// <param name="param">搜索条件</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> FlowAssemblyConfigAttributeDetail(SearchModel param)
        {
            Console.WriteLine("\n获取组件属性配置详情：FlowAssemblyConfigAttributeDetail");

            #region 参数校验

            Console.WriteLine($"Id：{param.Id}");
            if (string.IsNullOrEmpty(param.Id))
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            var result = await BisFlowAssemblyConfigAttributeDao.QuerySingle(w => w.Del_Flag == 0 && w.Id.Equals(param.Id));

            return new JsonResult(new ResultModel
            {
                Data = result
            });
        }

        #endregion

        #region 提交组件属性配置信息

        /// <summary>
        /// 提交组件属性配置信息
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> SubmitFlowAssemblyConfigAttribute(UpdateModel param)
        {
            Console.WriteLine("\n提交组件属性配置信息：SubmitFlowAssemblyConfigAttribute");

            #region 参数、数据校验

            Console.WriteLine($"FlowAssemblyConfigAttributeData：{param.FlowAssemblyConfigAttributeData}");

            if (param.FlowAssemblyConfigAttributeData == null)
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion 

            #region 获取所需数据

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            string orgId = string.Empty;
            string orgName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
                orgId = Accessor.HttpContext.User.Claims.First(i => i.Type == "orgId").Value;
                orgName = Accessor.HttpContext.User.Claims.First(i => i.Type == "orgName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            #endregion

            #region 数据提交

            DateTime now = DateTime.Now;
            Guid guid = Guid.NewGuid();
            string newId = guid.ToString().ToUpper();

            int result = 0;
            if (string.IsNullOrEmpty(param.FlowAssemblyConfigAttributeData.Id))
            {
                // 填充数据
                param.FlowAssemblyConfigAttributeData.Id = newId;
                param.FlowAssemblyConfigAttributeData.OrgGuid = orgId;
                param.FlowAssemblyConfigAttributeData.OrgName = orgName;
                param.FlowAssemblyConfigAttributeData.CreateUser = userId;
                param.FlowAssemblyConfigAttributeData.CreateUserName = userName;
                param.FlowAssemblyConfigAttributeData.CreateTime = now;
                param.FlowAssemblyConfigAttributeData.UpdateUser = userId;
                param.FlowAssemblyConfigAttributeData.UpdateUserName = userName;
                param.FlowAssemblyConfigAttributeData.UpdateTime = now;
                param.FlowAssemblyConfigAttributeData.Del_Flag = 0;

                result = await BisFlowAssemblyConfigAttributeDao.Insert(param.FlowAssemblyConfigAttributeData);

                return new JsonResult(new ResultModel
                {
                    Msg = "新增成功",
                    Data = result
                });
            }
            else
            {
                // 获取详情
                var detail = await BisFlowAssemblyConfigAttributeDao.QuerySingle(w => w.Del_Flag == 0 && w.Id.Equals(param.FlowAssemblyConfigAttributeData.Id));
                if (detail == null)
                {
                    return new JsonResult(new ResultModel
                    {
                        Success = false,
                        Msg = "未找到此数据"
                    });
                }

                // 填充数据
                param.FlowAssemblyConfigAttributeData.OrgGuid = detail.OrgGuid;
                param.FlowAssemblyConfigAttributeData.OrgName = detail.OrgName;
                param.FlowAssemblyConfigAttributeData.CreateUser = detail.CreateUser;
                param.FlowAssemblyConfigAttributeData.CreateUserName = detail.CreateUserName;
                param.FlowAssemblyConfigAttributeData.CreateTime = detail.CreateTime;
                param.FlowAssemblyConfigAttributeData.UpdateUser = userId;
                param.FlowAssemblyConfigAttributeData.UpdateUserName = userName;
                param.FlowAssemblyConfigAttributeData.UpdateTime = now;
                param.FlowAssemblyConfigAttributeData.Del_Flag = 0;

                result = await BisFlowAssemblyConfigAttributeDao.Update(param.FlowAssemblyConfigAttributeData);

                return new JsonResult(new ResultModel
                {
                    Msg = "编辑成功",
                    Data = result
                });
            }

            #endregion 数据提交
        }

        #endregion

        #region 批量删除组件属性配置信息

        /// <summary>
        /// 批量删除组件属性配置信息
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> DelFlowAssemblyConfigAttribute(UpdateModel param)
        {
            Console.WriteLine("\n批量删除组件属性配置信息：DelFlowAssemblyConfigAttribute");

            #region 参数效验

            Console.WriteLine($"Id：{param.Id}");

            if (string.IsNullOrEmpty(param.Id))
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            DateTime now = DateTime.Now;
            int result = await BisFlowAssemblyConfigAttributeDao.Update(u => new bis_flow_assembly_config_attribute
            {
                Del_Flag = 1,
                UpdateUser = userId,
                UpdateUserName = userName,
                UpdateTime = DateTime.Now
            }, w => param.Id.Contains(w.Id));

            return new JsonResult(new ResultModel
            {
                Msg = "删除成功",
                Data = result
            });
        }

        #endregion

        #region 更新组件属性配置信息状态

        /// <summary>
        /// 更新组件属性配置信息状态
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> UpdateFlowAssemblyConfigAttributeStstus(UpdateModel param)
        {
            Console.WriteLine("\n更新组件属性配置信息状态：UpdateFlowAssemblyConfigAttributeStstus");

            #region 参数效验

            Console.WriteLine($"Id：{param.Id}");
            Console.WriteLine($"Status：{param.Status}");

            if (string.IsNullOrEmpty(param.Id))
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            DateTime now = DateTime.Now;
            int result = await BisFlowAssemblyConfigAttributeDao.Update(u => new bis_flow_assembly_config_attribute
            {
                Status = param.Status,
                UpdateUser = userId,
                UpdateUserName = userName,
                UpdateTime = DateTime.Now
            }, w => param.Id.Contains(w.Id));

            return new JsonResult(new ResultModel
            {
                Msg = "提交成功",
                Data = result
            });
        }

        #endregion

        #region 更新组件属性配置信息顺序

        /// <summary>
        /// 更新组件属性配置信息顺序
        /// </summary>
        /// <param name="param">更新参数集</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> UpdateFlowAssemblyConfigAttributeSort(UpdateModel param)
        {
            await Task.Run(() => { Console.WriteLine("\n更新组件属性配置信息顺序：UpdateFlowAssemblyConfigAttributeSort"); });

            #region 参数效验

            Console.WriteLine($"SetValueData：{param.SetValueData}");

            if (param.SetValueData == null)
            {
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "必填参数不能为空"
                });
            }

            #endregion

            #region 获取登录用户相关信息

            string userId = string.Empty;
            string userName = string.Empty;
            try
            {
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userName = Accessor.HttpContext.User.Claims.First(i => i.Type == "userName").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            DateTime now = DateTime.Now;

            int result = 0;
            Parallel.ForEach(param.SetValueData, async item =>
            {
                result += await BisFlowAssemblyConfigAttributeDao.Update(u => new bis_flow_assembly_config_attribute
                {
                    Sort = int.Parse(item.Value)
                }, w => w.Id.Equals(item.Id));
            });

            return new JsonResult(new ResultModel
            {
                Msg = "提交成功",
                Data = result
            });
        }

        #endregion

        #region 获取可用组件数据

        /// <summary>
        /// 获取可用组件数据
        /// </summary>
        /// <param name="param">搜索条件</param>
        /// <returns>JSON结果集</returns>
        public async Task<JsonResult> FlowAssemblyData(SearchModel param)
        {
            Console.WriteLine("\n获取可用组件数据：FlowAssemblyData");

            #region 获取登录用户相关信息

            string orgId = string.Empty;
            string userId = string.Empty;
            string userRoleId = string.Empty;
            try
            {
                orgId = Accessor.HttpContext.User.Claims.First(i => i.Type == "orgId").Value;
                userId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userId").Value;
                userRoleId = Accessor.HttpContext.User.Claims.First(i => i.Type == "userRoleId").Value;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new JsonResult(new ResultModel
                {
                    Success = false,
                    Msg = "Token已过期, 请重新登录"
                });
            }

            #endregion

            // 获取相关数据
            var ClassifyData = await BisFlowAssemblyClassifyDao.QueryByWhere(w => w.Del_Flag == 0 && "0".Equals(w.Status), o => o.Sort);
            var ConfigData = await BisFlowAssemblyConfigDao.QueryByWhere(w => w.Del_Flag == 0 && "0".Equals(w.Status), o => o.Sort);
            var AttributeData = await BisFlowAssemblyConfigAttributeDao.QueryByWhere(w => w.Del_Flag == 0 && "0".Equals(w.Status), o => o.Sort);

            // 处理返回数据
            List<bis_flow_assembly_config> TempConfigData = new List<bis_flow_assembly_config>();
            List<bis_flow_assembly_config_attribute> TempAttributeData = new List<bis_flow_assembly_config_attribute>();
            ClassifyData.ForEach(item =>
            {
                // 获取当前分类关联组件数据
                TempConfigData = ConfigData.Where(w =>w.ClassifyId.Equals(item.Id)).ToList();
                if (TempConfigData != null && TempConfigData.Count > 0)
                {
                    TempConfigData.ForEach(itemSon =>
                    {
                        // 获取当前组件关联配置数据
                        TempAttributeData = AttributeData.Where(w => w.AssemblyConfigId.Equals(itemSon.Id)).ToList();
                        itemSon.AttributeList = TempAttributeData;
                    });
                }
                item.AssemblyList = TempConfigData;
            });

            return new JsonResult(new ResultModel
            {
                Data = ClassifyData
            });
        }

        #endregion

    }
}